<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>货物价格管理页面</title>
    <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">

    <style>
        body {
            margin:0;
            padding:0;
            font-family:sans-serif;
            background:#2C2255;
            background:url(../image/水漫漫.jpg);
            background-repeat: no-repeat;
            background-size:100%;
            font-size: 12px;
            font-family:STXingkai;
            color: #59ec0a;
        }
        a{
            text-decoration:none;
        }
        button{
            margin:10px;
        }
        th{
            width:300px;

        }
        td{
            width:200px;
            height:80px;
        }
    </style>
</head>
<body>
<h1>货物价格管理页面</h1>
<div id="matterInfo">
    <table id="matterInfo-table" >
    <!--        表格数据导出为excel表 通过js代码创建form表单传递参数实现条件查询-->
        <button  class="btn btn-warning "  @click="exportButton()">导出(Excel)</button>

        <!-- 文件上传的表单 -->
        <!--<form id="form"  style="display:none">
            上传文件：<input type="file" name="uploadFile">
            <button  id="import-btn" @click="importButton()">立即导入</button>
        </form>-->
        <input id="cert" type="file" />
        <input type="button" value="上传" onclick="submit2();" />




        <button data-toggle="modal" data-target="#myModal" class="btn btn-warning modify"  id="insert-customer" >添加新货物</button>
        <input id="itemsName" placeholder="请输入货物名称">
        <input id="itemsProduction"  placeholder="请输入产地">
        <button @click="selMatterInfoBYCondition()" class="btn btn-warning">查询货物</button>
        <button @click="delMatterInfo()" id="delMatterPrice" class="btn btn-danger">删除货物</button>
        <tr>
            <th ><input id="checkAll" type="checkbox" @click="allCheck"></th>
            <th>货物编号</th>
            <th>货物名称</th>
            <th>货物产地</th>
            <th>货物规格</th>
            <th>货物可供数量</th>
            <th>货物采购合同号</th>
            <th>货物加工合同号</th>
            <th>货物运输合同号</th>
            <th>货物入库合同号</th>
            <th>操作</th>
        </tr>
        <tr v-for="matterInfo in list" @click="trCheck(event)">
            <td ><input class="check" type="checkbox" :value="matterInfo.itemsInfoNo" @click="isCheckAll"></td>
            <td >{{matterInfo.itemsInfoNo}}</td>
            <td >{{matterInfo.itemsName}}</td>
            <td ><input type="text" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsProduction"></td>
            <td ><input type="text" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsScale"></td>
            <td ><input type="number" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsCounts"></td>
            <td ><input type="text" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsOrderNo"></td>
            <td ><input type="text" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsAddNo"></td>
            <td ><input type="text" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsTtranNo"></td>
            <td ><input type="text" style="border:none;background-color:transparent;color: #59ec0a;" v-model="matterInfo.itemsInStoreNo"></td>
            <td>
                <button v-on:click="updMatterInfo(matterInfo)" class="btn btn-danger ">修改货物信息</button>
            </td>
        </tr>
    </table>
    <!--            分页区域-->
    <div class="pagination-div">
        <ul class="pagination" id="page-ul" >
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);" @click="cutPage(1)">首页</a>
            </li>
            <li class="page-item" v-if="page.pageNow!=1">
                <a class="page-link" href="javascript:void(0);"
                   @click="cutPage(page.pageNow-1)">{{page.pageNow-1}}</a></li>
            <li class="page-item active">
                <a class="page-link" href="javascript:void(0);">{{page.pageNow}}</a>
            </li>
            <li class="page-item" v-if="page.pageNow!=page.pageTotal">
                <a class="page-link" href="javascript:void(0);"
                   @click="cutPage(page.pageNow+1)">{{page.pageNow+1}}</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);"
                   @click="cutPage(page.pageTotal)">尾页</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);">共有{{page.count}}条数据</a>
            </li>
            <li class="page-item">
                <a class="page-link" href="javascript:void(0);">共有{{page.pageTotal}}页</a>
            </li>
        </ul>
    </div>

    <!-- 模态框（Modal） -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog"  >
            <div class="modal-content" id="myModal-smile" style=" height: 800px;margin: 0">
                <div class="modal-body" >
                    <h1>添加货物详细信息</h1>
                    <form id="matterInfo-form">
                        <div class="form-group">
                            <label for="itemsName">货物名称:</label>
                            <input type="text" class="form-control"  name="itemsName"  >
                        </div>

                        <div class="form-group">
                            <label for="itemsProduction">货物产地：</label>
                            <input type="text" class="form-control"  name="itemsProduction" >
                        </div>

                        <div class="form-group">
                            <label for="itemsScale">货物规格：</label>
                            <input type="number" class="form-control" id="itemsScale" name="itemsScale" >
                        </div>


                        <div class="form-group">
                            <label for="itemsCounts">货物可供数量：</label>
                            <input type="text" class="form-control" id="itemsCounts" name="itemsCounts">
                        </div>

                        <div class="form-group">
                            <label for="itemsOrderNo">货物采购合同号：</label>
                            <input type="text" class="form-control" id="itemsOrderNo" name="itemsOrderNo">
                        </div>

                        <div class="form-group">
                            <label for="itemsAddNo">货物加工合同号：</label>
                            <input type="text" class="form-control" id="itemsAddNo" name="itemsAddNo">
                        </div>

                        <div class="form-group">
                            <label for="itemsTtranNo">货物运输合同号：</label>
                            <input type="text" class="form-control" id="itemsTtranNo" name="itemsTtranNo">
                        </div>

                        <div class="form-group">
                            <label for="itemsInStoreNo">货物入库合同号：</label>
                            <input type="text`" class="form-control" id="itemsInStoreNo" name="itemsInStoreNo">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" id="refresh" class="btn btn-default" @click="close()" data-dismiss="modal">提交并关闭</button>
                </div>
            </div>
        </div>
    </div>
</div>

<!--引入js文件-->
<script type="text/javascript" src="../css/jquery-3.4.1.js"></script>
<script src="../bootstrap/js/bootstrap.js" ></script>
<script src="../vue/vue.js" charset="utf-8"></script>
<script type="text/javascript" src="../css/my.js"></script>

<script>



    //通过form表单导出excel的方法
    function postExcelFile(params, url) { //params是post请求需要的参数，url是请求url地址
        var form = document.createElement("form");
        form.style.display = 'none';
        form.action = url;
        form.method = "post";
        document.body.appendChild(form);

        for(var key in params){
            var input = document.createElement("input");
            input.type = "hidden";
            input.name = key;
            input.value = params[key];
            form.appendChild(input);
        }

        form.submit();
        form.remove();
    }




    function submit2(){
        var type = "file";          //后台接收时需要的参数名称，自定义即可
        var id = "cert";            //即input的id，用来寻找值
        var formData = new FormData();
        formData.append(type, $("#"+id)[0].files[0]);    //生成一对表单属性
        $.ajax({
            type: "POST",           //因为是传输文件，所以必须是post
            url: '../MatterInfoController/importExcel',         //对应的后台处理类的地址
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                alert(data);
            }
        });
    }


    let matterInfoTabVue

    // 查询客户 需要接受一个当前页参数
    function selMatterInfo(pageNow,itemsName,itemsProduction) {
        let map = [];
        $.ajax({
            type: "post",
            url: "../MatterInfoController/selMatterInfo",
            dataType: "json",
            data:{pageNow:pageNow, pageCount:5,itemsName:itemsName,itemsProduction:itemsProduction},
            async: false,
            success: function (data) {
                map = data;
            }
        });
        return map;
    }
    /*创建一个map装查询到的第一页的数据*/
    let map = selMatterInfo(1,null,null);
    console.log(map);
    //加载分页数据
    matterInfoTabVue = new Vue({
        el: '#matterInfo',
        data: {
            list: map.list,// 展示表格
            page: map // 用于分页
        },
        methods: {
            //切换页方法 参数为当前按钮所代表的对应页
            cutPage: function (pageNow) {
                let itemsName = $("#itemsName").val();
                let itemsProduction = $("#itemsProduction").val();
                //查询要切换页的数据。把结果从新给表格赋值
                matterInfoTabVue.list = selMatterInfo(pageNow,itemsName,itemsProduction).list;
                //把分页的vue重新赋值。
                matterInfoTabVue.page.pageNow = pageNow;
            },
            //导入代码区域
            importButton:function () {
                $.ajax({
                    url : "../MatterInfoController/importExcel",
                    data : new FormData($("#form")[0]),
                    type : "POST",
                    // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                    processData : false,
                    // 告诉jQuery不要去设置Content-Type请求头
                    contentType : false,
                    dataType : "json",
                    success : function(json) {
                        alert(json.msg);
                        window.location.reload();
                    },
                    error : function(json) {
                        alert("导入失败！");
                    }
                });

            },
            exportButton:function () {
                alert(111);
                let itemsName = $("#itemsName").val();
                let itemsProduction = $("#itemsProduction").val();
                var params = {itemsName:itemsName,itemsProduction:itemsProduction};
                postExcelFile(params, "../MatterInfoController/exportCustomerExcel");
            },
            //模态框关闭提交修改
            close:function () {
                console.log("表格中的内容"+$("#matterInfo-form").serialize());
                $.ajax({
                    type:"post",
                    url:"../MatterInfoController/addMatterInfo",
                    data:$("#matterInfo-form").serialize(),
                    dataType:"json",
                    success:function (json) {
                        alert(json.msg);
                        window.location.reload();
                    }
                })
            },
            //条件查询重新调用查询方法
            selMatterInfoBYCondition:function(){
                //获取两个input框的数据
                let itemsName = $("#itemsName").val();
                let itemsProduction = $("#itemsProduction").val();
                matterInfoTabVue.list = selCustomer(1,itemsName,itemsProduction).list;
                matterInfoTabVue.page = selCustomer(1,itemsName,itemsProduction);
                console.log(matterInfoTabVue.page);
            },
            allCheck:function () {
                eventHub.$emit("allCheck",null);
            },
            isCheckAll:function () {
                eventHub.$emit("isCheckAll",null);
            },
            trCheck:function (event) {
                eventHub.$emit("trCheck",event);
            },
            delmatterInfo:function () {
                let mIds = '';
                //获取被选中的复选框的id值 each()遍历
                $(".check").each(function () {
                    //对所有符合该选择器的元素进行操作
                    //判断该复选框是否选中
                    if(this.checked){
                        mIds+=this.value + ",";
                        if(mIds=="") return;
                        $.ajax({
                            type: "post",
                            url: "../MatterInfoController/delMatterInfo",
                            data: {mIds: mIds},
                            dataType: "json",
                            success: function (data) {
                                alert(data.msg);
                                window.location.reload();
                            }
                        });
                    }
                });
            },
            updMatterInfo:function (matterInfo) {
                $.ajax({
                    type: "post",
                    url: "../MatterInfoController/updMatterInfo",
                    data: matterInfo,
                    dataType: "json",
                    success: function (data) {
                        alert(data.msg);
                        window.location.reload();
                    }
                });
            }
        }
    });

</script>
</body>
</html>